// pages/user/user.js
import request from '../../request/request.js'
const APPId = "xx";
const app_Secret = "xxx";

Page({
  data: {
    userInfo: {}
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
      
  },

  onShow: function () {
    const userInfo=wx.getStorageSync("userInfo");
    if(userInfo){
      this.setData({userInfo:userInfo})
    }
  },
  login() {
    wx.getUserProfile({
      desc: '获取授权',
      success: (res) => {
        this.setData({ "userInfo": res.userInfo })
        wx.login({
          success: (data) => {
            wx.request({
              //获取openid接口
              url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + APPId + '&secret=' + app_Secret + '&js_code=' + data.code + '&grant_type=authorization_code',
              data: {},
              method: 'GET',
              success: (res) => {
                this.setData({ "userInfo.openId": res.data.openid })
                //发送请求保存用户数据
                request({
                  url: "user/save",
                  data: this.data.userInfo,
                  method: "post",
                  tag:'login'
                }).then(res=>{
                  this.setData({ userInfo: res.data.data})
                  wx.setStorageSync("userInfo", this.data.userInfo);
                }).catch(err=>{})
              }
            })
          }
        })
      }
    })
  },
  loginOut(){
    wx.showModal({
      title: '提示',
      content: '您确定要退出吗',
      success: (res) =>{
        if (res.confirm) {
          this.setData({userInfo:{}})
          wx.setStorageSync('userInfo', {})
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  about(){
    wx.showToast({
      title: '成都信息工程大学：陈澳',
      icon: 'none',
      duration: 1500,
      mask: false,
    });
  },
  balance(){
    wx.showToast({
      title: '充值请联系管理员',
      icon: 'none',
      duration: 2500,
      mask: false,
    });
  }
})